package com.hxxzt.hikari.controller;

import com.hxxzt.hikari.common.exception.NikoException;
import com.hxxzt.hikari.common.vo.ResultVo;
import com.hxxzt.hikari.entity.User;
import com.hxxzt.hikari.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-01-13
 */
@RestController
@RequestMapping("/hikari/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    private String message;

    @Autowired
    private IUserService userService;


    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询User表中所有数据")
    public ResultVo queryAll() {
        return ResultVo.oK(userService.queryAll());
    }

    /**
     * 通过Id查询
     *
     * @param id 自增id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过Id查询User表中数据")
    public ResultVo queryById(@PathVariable("id") Integer id) {
        return ResultVo.oK(userService.queryById(id));
    }

    /**
     * 根据条件查询
     */
    @GetMapping("/select")
    @ApiOperation(value = "按照条件查询User表中数据")
    public ResultVo queryById(User user) {
        return ResultVo.oK(userService.queryByUser(user));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询User表中数据")
    public ResultVo queryById(@RequestParam Integer page, @RequestParam Integer limit) {
        return ResultVo.oK(userService.queryPage(page, limit));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据Id删除User表中数据")
    public ResultVo deleteById(@PathVariable("id") Integer id) throws NikoException {
        try {
            userService.deleteById(id);
            return ResultVo.oK();
        } catch (Exception e) {
            message = "删除用户失败";
            log.error(message, e);
            throw new NikoException(message);
        }
    }

    @PostMapping
    @ApiOperation(value = "新增User表中数据")
    public ResultVo insert(@RequestBody User user) throws NikoException {
        try {
            userService.insert(user);
            return ResultVo.oK();
        } catch (Exception e) {
            message = "新增用户失败";
            log.error(message, e);
            throw new NikoException(message);
        }
    }

    @PutMapping
    @ApiOperation(value = "修改User表中数据")
    public ResultVo update(@RequestBody User user) throws NikoException {
        try {
            userService.updateById(user);
            return ResultVo.oK();
        } catch (Exception e) {
            message = "修改用户失败";
            log.error(message, e);
            throw new NikoException(message);
        }
    }

    @GetMapping("/parent")
    @ApiOperation(value = "查询全部树状图")
    public ResultVo parent() throws NikoException {

        return ResultVo.oK(userService.findSon());
    }
    @GetMapping("/parent/{userId}")
    @ApiOperation(value = "通过Id查询下级，0为所有父级")
    public ResultVo parent(@PathVariable(value = "userId") Integer userId) throws NikoException {

        return ResultVo.oK(userService.findSonByUserId(userId));
    }

}
